<template>
  <div class="app-container flex flex-col">
    <div class="flex items-center">
      <el-radio-group v-model="activeName" size="small" class="mr-2">
        <el-radio-button
          v-for="(item, index) in tabs"
          :key="item"
          :index="index"
          :label="item"
        ></el-radio-button>
      </el-radio-group>
      <el-button size="mini" style="height: 30px" @click="">
        copy last groups
      </el-button>
      <el-button size="mini" style="height: 30px" @click="">
        copy url
      </el-button>
    </div>
    <div class="flex-1 relative">
      <template v-if="activeName === 'line'">
        <RangeLine
          :key="key_01"
          :show-line-type="5"
          :series-data="seriesData"
        ></RangeLine>
      </template>
      <template v-else-if="activeName === 'kill prev'">
        <!-- <RangeLine :key="key_02" :show-line-type="5"></RangeLine> -->
      </template>
    </div>
  </div>
</template>
<script>
import _ from 'lodash'
import DateLineViewMixin from '@/mixins/DateLineViewMixin.js'

export default {
  name: 'HotCold',
  mixins: [DateLineViewMixin],
  data() {
    return {
      seriesData: [],
      tabs: ['line', 'kill prev'],
      activeName: 'line' // or kill prev
    }
  },
  created() {
    this.calcHotCold()
  },
  methods: {
    // 计算热号冷号
    calcHotCold() {
      const data = _.cloneDeep(this.allValues)

      const values = data.map((item, index) => {
        // 每次从0截取到当前index
        const currentData = data.slice(0, index)

        // 初始化所有号码的计数为 0，并确保键是 "01" 到 "49" 的格式
        const allNumbersCount = _.chain(_.range(1, 50))
          .map((number) => [_.padStart(number.toString(), 2, '0'), 0])
          .fromPairs()
          .value()

        // 统计每个号码出现的次数
        const numberCount = _.countBy(currentData, 'number')

        // 将实际计数与初始化的计数合并，未出现的号码将保持计数为 0
        _.assign(allNumbersCount, numberCount)

        // 是否开始统计热号
        const isStart = _.chain(allNumbersCount)
          .filter((value, key) => value > 1)
          .size()
          .gt(24)
          .value()

        // 转换 allNumbersCount 为数组并降序排序
        const sortedNumbers = _(allNumbersCount)
          .map((count, number) => ({ number, count })) // 转换为对象数组
          .orderBy(['count'], ['desc']) // 根据 count 降序排序
          .value()

        // 热号
        let hotNumbers = []
        // 冷号
        let coldNumbers = []

        let value = 0

        if (isStart) {
          hotNumbers = sortedNumbers.slice(0, 25).map((item) => item.number)
          coldNumbers = sortedNumbers.slice(25).map((item) => item.number)

          value = hotNumbers.includes(item.number) ? 1 : 0
        }
        return {
          ...item,
          hotNumbers,
          coldNumbers,
          allNumbersCount,
          sortedNumbers,
          value
        }
      })
      navigator.clipboard.writeText(`
        ${JSON.stringify(values, null, 4)}
      `)
      this.seriesData = values
    }
  }
}
</script>
